package init

import (
	"fmt"
	"github.com/spf13/viper"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"two_exam/config"
)

func init() {
	InitConfig()
	InitDB()
}
func InitConfig() {
	viper.SetConfigFile("D:\\goWork\\homework\\two-exam\\dev.yaml")
	viper.ReadInConfig()
	viper.Unmarshal(&config.GlobalConfig)
	fmt.Println(config.GlobalConfig)
}

var err error

func InitDB() {

	appConfig := config.GlobalConfig.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=true", appConfig.User, appConfig.Password, appConfig.Host, appConfig.Port, appConfig.Database)
	config.DB, err = gorm.Open(mysql.Open(dsn))
	if err != nil {
		panic(err)
	}
	fmt.Println("MySQL连接成功")
}
